package com.qg.lizhanqi.videosquare.adaputer;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.qg.lizhanqi.videosquare.R;
import com.qg.lizhanqi.videosquare.bean.VideoInfors;
import com.qg.lizhanqi.videosquare.uitlhelp.ImageLoader;
import com.qg.lizhanqi.videosquare.uitlhelp.MyUtils;

import java.util.HashMap;
import java.util.List;
/**
 * Created by lizhanqi on 2016-8-17-0017.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyHolder> {
    private final String url = "http://101.200.142.201:8080/VideoPlay/video";
    private static final String TAG = "李站旗";
    Context context;
    List<VideoInfors.VideoListBean> listVideo;
    LayoutInflater mInflater;
    //回调
    private onItemClickListener mOnIemClickListener;
    public void setOnItemClinckListener(onItemClickListener listener) {
        this.mOnIemClickListener = listener;
    }
    public interface onItemClickListener {
        void onItemClinck(View view, int position);
        void onItemLoingClinck(View view, int position);
    }
    public RecyclerAdapter(Context context, List<VideoInfors.VideoListBean> listVideo) {
        this.context = context;
        this.listVideo = listVideo;
        this.mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getItemCount() {
        return listVideo.size();
    }
    /*创建ViewHolder*/
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View viewItem = mInflater.inflate(R.layout.video_item_layout, parent, false);
        MyHolder myHolder = new MyHolder(viewItem);
        return myHolder;
    }
    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        VideoInfors.VideoListBean videoInfo = listVideo.get(position);
        holder.tv_date.setText(videoInfo.getVideotime());
        holder.tv_issuer.setText(videoInfo.getUsername());
         String path = url + videoInfo.getVideourl();
        //图片
        ImageButton videoImage = holder.videoImage;
        videoImage.getLayoutParams().height = MyUtils.GetScreenWidth(context) /5;
        videoImage.getLayoutParams().width = MyUtils.GetScreenWidth(context) / 5;
        videoImage.setTag(path);
        Log.i(TAG, "onBindViewHolder: 应该加载的图片路径是："+path);
        ImageLoader instance = ImageLoader.getInstance();
        instance.defaultConfig(context);
        instance.displayImage(path,videoImage);
     /*   new AsyncTask<String,Void,Bitmap>(){
            @Override
            protected Bitmap doInBackground(String... params) {
                return   createVideoThumbnail(params[0],300,300);
        }
            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                holder.videoImage.setImageBitmap(bitmap);
            }
        }.execute(path);*/
         //增加item点击的回调
        if (mOnIemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnIemClickListener.onItemClinck(holder.itemView, position);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnIemClickListener.
                            onItemLoingClinck(holder.itemView, position);
                    return false;
                }
            });
        }
    }
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private Bitmap createVideoThumbnail(String url, int width, int height) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        int kind = MediaStore.Video.Thumbnails.MINI_KIND;
        try {
            if (Build.VERSION.SDK_INT >= 14) {
                retriever.setDataSource(url, new HashMap<String, String>());
            } else {
                retriever.setDataSource(url);
            }
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
            // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
            // Assume this is a corrupt video file.
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                // Ignore failures while cleaning up.
            }
        }
        if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }
    class MyHolder extends RecyclerView.ViewHolder {
        ImageButton videoImage;
        TextView tv_issuer;
        TextView tv_date;
        public MyHolder(View itemView) {
            super(itemView);
            /*初始化找到控件*/
            videoImage = (ImageButton) itemView.findViewById(R.id.videoImage);
            tv_issuer = (TextView) itemView.findViewById(R.id.tv_issuer);
            tv_date = (TextView) itemView.findViewById(R.id.tv_date);
        }
    }
}





